//- 先去这个pug文件中找
extends ../commons/commons.pug

block append pageStyle
	link(href="${relativePath}/static/css/shopping/shopIndex.css" rel="stylesheet" type="text/css")
	link(href="${relativePath}/static/css/personCenter/securityCenter.css" rel="stylesheet" type="text/css")

block content
	//- 新建订单
	div.newOrderContent
		h3.orderTitle 安全中心
		div.setBox
			div.row.rowsOne
				label.topBox 手机号:
				div.right.fl
					span.set.fl 已设置
					span.phone.fl
			div.row.rowsOne
				label.topBox 密码:
				div.right.fl
					span.set.fl 已设置
					a.modify.dlmodify(href="javascript:;") 修改
			div.row.rowsOne
				label.topBox 支付密码:
				div.right.fl
					span.set.setZF.fl 
					a.modify.zfmodify(href="javascript:;") 修改
			div.row.rowsOne.myCount
				label.topBox 提现账户:
				div.right.rightbox.fl
					//- div.cardBox.fl
					//- 	div.list.fl
					//- 		span.cardname.fl 中国银行
					//- 		span.endNum.fl 尾号：0523
					//- 	a.del(href="javascript:;") 删除
			div.row.rowsOne
				label.topBox &nbsp;&nbsp;&nbsp;&nbsp;
				div.addCard.fl 添加银行
				div.addZFB.fl 添加支付宝
			//- 添加银行卡
			div.bankBox
				form#bank
					div.row
						label &nbsp;&nbsp;&nbsp;添加银行：
						select.select(name="bank")
							option 请选择
					div.row
						label 持卡人姓名：
						input.cardName.txt(name="cardName")
					div.row
						label &nbsp;&nbsp;&nbsp;身份证号：
						input.txt.idCard(name="idCard")
					div.row
						label &nbsp;&nbsp;&nbsp;银行卡号：
						input.txt.bankNum(name="bankName")
					div.row
						label &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;手机号：
						input.txt.phoneNum(placeholder="办卡预留手机号" name="phoneNum")
					div.row
						label.fl 短信验证码：
						div.codeM.fl
							input.inpTxt.phoneCode.fl(type="text" name="phoneCode")
							input.inpTxt.passCode(type="button" value="获取短信验证码")
					div.row.saveBtn
						a#save(href="javascript:;") 保存
						a#del(href="javascript:;") 取消	
					//- 添加支付宝
				form#zfbBox
					div.row
						label &nbsp;&nbsp;&nbsp;真实姓名：
						input.cardName.cardName1.txt(name="cardName1")
					div.row
						label 支付宝账号：
						input.zfbCount.zfbCount1.txt(name="zfbCount1")
					div.row
						label &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;手机号：
						input.txt.phoneNum1(placeholder="办卡预留手机号" name="phoneNum1")
					div.row
						label.fl 短信验证码：
						div.codeM.fl
							input.inpTxt.phoneCode1.fl(type="text" name="phoneCode1")
							input.inpTxt.passCode1(type="button" value="获取短信验证码")
					div.row.saveBtn
						a#save1(href="javascript:;") 保存
						a#del1(href="javascript:;") 取消	


block append pageScript
	script(src="${relativePath}/static/js/jquery.validate.min.js")
	script(src="${relativePath}/static/js/messages_zh.js")
	script(type="text/javascript" src="${relativePath}/static/js/card.js")

	script.
		$(function() {
			
			//- 获取银行卡列表
			$.post(shopIp+"/cash/bankList",{
				userToken:token
			},function(data) {
				var lists = data.data;
				for(var i=0;i<lists.length;i++) {
					var optEle = "<option>"+lists[i].name+"</option>";
					//console.log(optEle);
					$("#bank .select").append(optEle);
				}	
			})

			//- 验证添加银行卡
			//- 电话号码验证
			jQuery.validator.addMethod("isMobile", function(value, element) {
				var length = value.length;
				var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
				return this.optional(element) || (length == 11 && mobile.test(value));
			}, "请正确填写您的手机号码");

			// 身份证号码验证
			jQuery.validator.addMethod("isIdCardNo", function(value, element) {
				//var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
				return this.optional(element) || idCardNoUtil.checkIdCardNo(value);
			}, "请正确输入您的身份证号码");


			var validator1 = $("#bank").validate({
				rules: {
					cardName:{
						required:true
					},
					bank:{
						required:true
					},
					idCard:{
						isIdCardNo:true,
						required:true,
					},
					bankName:{
						bankCardNum:true,
						required:true,
						number:true
					},
					phoneNum:{
						required:true,
						isMobile:true
					},
					phoneCode:{
						required:true,
						number:true
					}
				},
				messages : {
					cardName:{
						required:"请输入持卡人姓名"
					},
					bank:{
						required:"请选择银行卡"
					},
					idCard:{
						required:"请输入身份证号",
					},
					bankName:{
						required:"请输入银行卡号"
					},
					phoneNum:{
						required:"请输入办卡预留手机号"
					},
					phoneCode:{
						required:"请输入验证码"
					}
			    },
				errorPlacement: function(error, element) {  
					error.appendTo(element.closest(".row"));
				}
			});
			//- 验证支付宝
			var validator2 = $("#zfbBox").validate({
				rules: {
					cardName1:{
						required:true
					},
					//- zfbCount1:{
					//- 	required:true
					//- },
					phoneNum1:{
						isMobile:true,
						required:true,
					},
					phoneCode1:{
						required:true,
						number:true
					}
				},
				messages : {
					cardName1:{
						required:"请输入持卡人姓名"
					},
					phoneNum1:{
						required:"请输入办卡预留手机号"
					},
					phoneCode1:{
						required:"请输入验证码"
					}
			    },
				errorPlacement: function(error, element) {  
					error.appendTo(element.closest(".row"));
				}
			});
			//- 发送验证码
			var countdown=60;
			function settime(obj) {
			    if (countdown == 0) {
			        obj.removeAttribute("disabled");    
			        obj.value="获取验证码";
			        countdown = 60;
			        return;
			    } else {
			        obj.setAttribute("disabled", true);
			        obj.value="重新发送(" + countdown + ")";
			        countdown--;
			    }
			setTimeout(function() {
			    settime(obj) }
			    ,1000)
			}

			//- 点击发送验证码
			$(".passCode").click(function(){
				var phoneNum = $(".phoneNum").val();
				//console.log(phoneNum);
				var _this = this;
				if(phoneNum == ""){
					layer.msg("请输入手机号!");
					return false;
				}else{
					$.post(shopIp+"/Utils/sendCaptcha",{
						phone:$(".phoneNum").val(),
					},function(data){
						//console.log(data);
						if(data.status == "200"){
							settime(_this);
							setTimeout(function() {
								layer.msg(data.msg);
							},1000);
						}else{
							layer.msg(data.msg);
						}
					})
				}
			})
			//- 点击发送验证码
			$(".passCode1").click(function(){
				var phoneNum1 = $(".phoneNum1").val();
				//console.log(phoneNum);
				var _this = this;
				if(phoneNum1 == ""){
					layer.msg("请输入手机号!");
					return false;
				}else{
					$.post(shopIp+"/Utils/sendCaptcha",{
						phone:$(".phoneNum1").val(),
					},function(data){
						//console.log(data);
						if(data.status == "200"){
							settime(_this);
							setTimeout(function() {
								layer.msg(data.msg);
							},1000);
						}else{
							layer.msg(data.msg);
						}
					})
				}
			})
			//- 获取银行卡列表
			$.post(shopIp+"/cash/listAccount",{
				userToken:token
			},function(data) {
				var cards = data.data.account;
				for(var i=0;i<cards.length;i++) {
					var ele = '<div class="cardBox fl" id="'+cards[i].id+'"><div  class="list fl"><span class="cardname fl">'+cards[i].bank_name+'</span><span class="endNum fl"> 尾号：'+cards[i].bank_card.substr(-4,5)+'</span></div><a class="del" href="javascript:;">删除</a></div>';
					$(".rightbox").append(ele);
				}
				if(data.data.is_setPay == "0") {
					$(".setZF").html("未设置");
				}else{
					$(".setZF").html("已设置");
				}
				$(".phone ").html(data.data.phone);
			})

			//- 点击添加银行卡
			$(".addCard").click(function(){
				$("#bank").show();
			})
			//- 点击修改进入修改页面
			$(".zfmodify").click(function() {
				window.location.href = "modifyPsw.html"
			})
			$(".dlmodify").click(function() {
				window.location.href = "dlmodifyPsw.html"
			})
			//- 点击添加支付宝
			$(".addZFB").click(function(){
				$("#bank").hide();
				$("#zfbBox").show();
			})
			//- 点击添加银行卡保存
			$("#save").click(function() {
				if(validator1.form()) {
					$.post(shopIp+"/cash/addBankCard",{
						userToken:token,
						bank_name:$(".select").val(),
						real_name:$(".cardName").val(),
						identification:$(".idCard").val(),
						bank_card:$(".bankNum").val(),
						phone:$(".phoneNum").val(),
						type:2,
						sms_code:$(".phoneCode").val()
					},function(data) {
						if(data.status == 200) {
							layer.msg(data.msg);
							setTimeout(function() {
								location.reload();
							},1500);
						}else{
							layer.msg(data.msg);
						}
					})
				}
			})
			//- 点击支付宝取消
			$("#del1").click(function() {
				$("#zfbBox").hide();
				$(".cardName1").val("");
				$(".zfbCount1").val("");
				$(".phoneNum1").val("");
				$(".phoneCode1").val("");
				$("#zfbBox label.error").hide();
			})
			//- 点击添加支付宝
			$("#save1").click(function() {
				if(validator2.form()) {
					$.post(shopIp+"/cash/addBankCard",{
						userToken:token,
						bank_name:"支付宝",
						real_name:$(".cardName1").val(),
						bank_card:$(".zfbCount1").val(),
						phone:$(".phoneNum1").val(),
						type:1,
						sms_code:$(".phoneCode1").val()
					},function(data) {
						console.log(data);
						if(data.status == 200) {
							layer.msg(data.msg);
							setTimeout(function() {
								location.reload();
							},1500);
						}else{
							layer.msg(data.msg);
						}
						
					})
				}
			})
			//- 点击银行卡取消
			$("#del").click(function() {
				$("#bank").hide();
				$(".select").val("");
				$(".cardName").val("");
				$(".idCard").val("");
				$(".bankNum").val("");
				$(".phoneNum").val("");
				$(".phoneCode").val("");
				$("#bank label.error").hide();
			})

			//- 点击列表删除
			$(".rightbox").on("click",".del",function(){
				//alert("22");
				var $this = $(this);
				layer.confirm('您确定删除银行卡吗？', {
					btn: ['确定','取消'], //按钮
					title: ['删除提现账户', 'text-align:center;padding:0;']
				}, function(){
					$.post(shopIp+"/cash/deleteAccount",{
						userToken:token,
						id:$this.closest(".cardBox").attr("id")
					},function(data) {
						//console.log(data);
						if(data.status == 200) {
							layer.msg(data.msg, {icon: 1});
							$this.closest(".cardBox").remove();
						}else{
							layer.msg(data.msg);
						}
					})
				}, function(){
						//layer.msg('已取消');
				});
			})
		})
		
		